package com.scraper;

import org.jsoup.nodes.Element;

/**
 * This class holds common methods used in WebScraper. 
 * 
 * @author Micah Perry
 *
 */
public class WebScraperCommon {
	
	protected static Link constructLinkGivenTag(Element tagElement) {
		
		Link returnLink = null;

		String linkURL = retrieveElementAbsoluteHref(tagElement); 
		String linkDescription = retrieveElementText(tagElement); 
		
		linkURL = stringTrim(linkURL); 
		linkDescription = stringTrim(linkDescription);
		
		returnLink = new Link(linkURL, linkDescription);
		
		return returnLink;
	}
	
	protected static String stringTrim(String string) {
		String returnString = string.trim(); 
		returnString = returnString.replace(String.valueOf((char) 160), " ").trim();

		return returnString;
	}

	protected static String retrieveElementText(Element tagElement) {

		return tagElement.text();
	}
	
	protected static String retrieveElementAbsoluteHref(Element tagElement) {

		return tagElement.attr("abs:href");
	}
	
	protected static String retrieveElementNonAbsolute(Element tagElement) {
		String returnHref = "";  
		if(tagElement != null)
		{
			returnHref = tagElement.attr("href");
		}
		
		return returnHref;
	}
	
	protected static Link constructLinkGivenTagNonAbs(Element tagElement) {
	Link returnLink = null;

	String linkURL = WebScraperCommon.retrieveElementNonAbsolute(tagElement); 
	String linkDescription = WebScraperCommon.retrieveElementText(tagElement); 
	
	
	returnLink = new Link(linkURL, linkDescription);
	
	return returnLink;
	}

}
